#!/bin/bash

BASEDIR=/opt/workspace/cesm-indigo/cesm-ws/

function deploy_remote {
    scp $BASEDIR/cesm-ws-ear/target/cesm-ws-*.ear $TARGET_HOST:$TARGET_JBOSS/deploy/
    find $BASEDIR/service-dispatcher/target -name "*.jar" ! -name "*-sources.jar" -exec scp {} $TARGET_HOST:$TARGET_JBOSS/deploy/ \;
    find $BASEDIR/cesm-ws-util/target $BASEDIR/grid-submitter/target -name "*.jar" ! -name "*-sources.jar" -exec scp {} $TARGET_HOST:$TARGET_JBOSS/lib/ \;
    find $BASEDIR/grid-submitter/target -name "*.jar" ! -name "*-sources.jar" -exec scp {} $TARGET_HOST:$TARGET_JBOSS/deploy \;
}

function usage {
    echo "$0 {local|help|<target_hostname>}"
    echo "    local - deploy to localhost"
    echo "    <target_hostname> - deploy to <target_hostname>.rcac.purdue.edu"
    echo "    help - display this message"
}

function deploy_local {
    cp $BASEDIR/cesm-ws-ear/target/cesm-ws-*.ear $TARGET_JBOSS/deploy/
    cp $BASEDIR/service-dispatcher/target/service-dispatcher-*.jar $TARGET_JBOSS/deploy/
    cp $BASEDIR/cesm-ws-util/target/cesm-ws-util-*.jar $TARGET_JBOSS/lib/
    cp $BASEDIR/grid-submitter/target/grid-submitter-*.jar $TARGET_JBOSS/lib/

    rm $TARGET_JBOSS/deploy/*-sources.jar
    rm $TARGET_JBOSS/lib/*-sources.jar
}

case "$1" in
    ssg-prod3)
        TARGET_HOST=ssg-prod3
        TARGET_JBOSS=/opt/ssg-deploy/jboss-5.1.0.GA/server/default
        deploy_remote
        ;;
    tg-dev04)
        TARGET_HOST=tg-dev04
        TARGET_JBOSS=/opt/cesm/server/jboss-5.1.0.GA-jdk6/server/default
        deploy_remote
        ;;
    help)
        usage
        ;;
    local)
        TARGET_JBOSS=/opt/server/jboss-5.1.0.GA-jdk6/server/default
        deploy_local
esac
